<template>
  <div class='container'>
    <el-card class="box-card">
      <div slot="header" class="clearfix" v-if="subject.id && subject.name">
        <el-breadcrumb
          type="flex"
          align="middle"
          justify-content="center"
          separator-class="el-icon-arrow-right"
        >
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>{{ subject.name }}</el-breadcrumb-item>
          <el-breadcrumb-item>目录管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
    <!-- 头部 输入框 按钮 -->
    <el-row>
        <el-col :span="18">
          <el-form label-width="80px" size="small" :inline="true">
            <el-form-item label="目录名称">
              <el-input style="200px" v-model="form.directoryName"></el-input>
           </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="form.state">
                <el-option :value="1" label="启用"></el-option>
                <el-option :value="0" label="禁用"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item style="text-align:right">
              <el-button @click="clear()">清除</el-button>
              <el-button type="primary" @click="search">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6" style="text-align:right">
          <el-button v-if="subject.id && subject.name" type="text" icon="el-icon-back" @click="$router.back()">返回学科</el-button>
          <el-button icon="el-icon-edit" type="success" size="small" @click="showDialog">新增目录</el-button>
        </el-col>
      </el-row>
<!-- 消息提示 -->
      <el-alert
        :title="`数据一共 ${total} 条`"
        style="margin-bottom:15px"
        type="info"
        class="alert"
        :closable="false"
        show-icon
      ></el-alert>
 <!-- 表格数据 -->
      <el-table :data="directoryList" style="width: 100%">
            <el-table-column  type="index" label="序号" width="180"> </el-table-column>
            <el-table-column  prop="subjectName" label="所属学科" width="180"></el-table-column>
            <el-table-column  prop="directoryName" label="目录名称" width="180"></el-table-column>
            <el-table-column  prop="username" label="创建者" width="180"></el-table-column>
            <el-table-column  prop="addDate" label="创建日期" width="180">
              <template #default='{row}'>
              {{ row.addDate|parseTimeByString}}
              </template>
            </el-table-column>
            <el-table-column  prop="totals" label="面试题数量" width="180"></el-table-column>
            <el-table-column  prop="state" label="状态" width="180">
              <template #default='{row}'>
                {{ row.state === 1 ? '启用' : '禁用' }}
              </template>
            </el-table-column>
            <el-table-column  label="操作">
              <template #default='{row}'>
                  <!-- {{row.id}} -->
                <el-button type="text" @click="toggleState(row)">{{row.state===1?'禁用':'启用'}}</el-button>
                <el-button type="text" @click="showEditDialog(row)" :disabled="row.state===1">修改</el-button>
                <el-button type="text" @click="delDirectory(row)" :disabled="row.state===1||row.totals > 0">删除</el-button>
              </template>
            </el-table-column>
      </el-table>

      <!-- 分页 -->
      <!--  -->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="form.page"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="form.pageSize"
      layout="prev, pager, next, sizes, jumper"
      :total="total"
      background>
    </el-pagination>
  </el-card>
  <DirectorysAdd ref="addRef" :dialogVisible.sync="dialogVisible" @reload="getDirectorysList"></DirectorysAdd>
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/directorys.js'
import DirectorysAdd from '@/module-hmmm/components/directorys-add'
// import { status } from '@/api/base/baseApi.js'
export default {
  name: 'directory',
  components: {
    DirectorysAdd
  },
  data () {
    return {
      form: {
        directoryName: null,
        state: null,
        page: 1, // 当前页
        pageSize: 10 // 每页  条数
      },
      directoryList: [],
      total: null,
      name: null,
      dialogVisible: false
    }
  },
  created () {
    this.getDirectorysList()
  },
  methods: {
    async getDirectorysList () {
      // { page: this.currentPage4, pagesize: this.pageSize }
      // console.log(this.subject)
      if (this.subject.id) {
        this.form.subjectID = this.subject.id
      }
      const { data } = await list(this.form)
      // console.log(data)
      this.total = data.counts
      this.directoryList = data.items
    },
    //  清除
    clear () {
      this.form = {
        directoryName: null,
        state: null,
        page: 1,
        pageSize: 10
      }
    },
    // 切换是否禁用
    async toggleState (row) {
      await changeState({ id: row.id, state: row.state === 1 ? 0 : 1 })
      row.state = row.state === 1 ? 0 : 1
    },
    // 每页条
    handleSizeChange (val) {
      this.form.pageSize = val
      this.getDirectorysList()
    },
    //  当前页
    handleCurrentChange (val) {
      this.form.page = val
      this.getDirectorysList()
    },
    // 删除目录
    async delDirectory (row) {
      const str = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        type: 'warning'
      }).catch(() => {})
      if (str !== 'confirm') return this.$message.info('已取消')
      await remove({ id: row.id })
      this.getDirectorysList()
    },
    // 打开添加对话框
    showDialog () {
      this.dialogVisible = true
    },
    // 打开修改对话框
    showEditDialog (row) {
      this.dialogVisible = true
      this.$refs.addRef.directoryForm = row
    },
    // 搜索功能
    search() {
      this.form.page = 1
      this.getDirectorysList()
    }

  },
  computed: {
    subject () {
      return this.$route.query || {}
    }
  }
}
</script>

<style scoped lang='scss'>
.box-card {
    padding: 10px 20px;
  }
.el-pagination{
  float: right;
  margin: 10px 0;
}
</style>
